
var maanden = new Array('januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december');
var currentMonth = new Date().getMonth();
var currentYear = new Date().getFullYear();

$(document).ready(function(){
    $('#tooltip').css({
        opacity: 0
    });
    $('#tooltip').show();
    // stel eerst de data op...
    fillCalendar(currentMonth + 1, currentYear);
    // handel de events af..
    $('.calendarCell.prev').click(function(){
        currentMonth = (currentMonth + 11) % 12;
        if(currentMonth == 11){
            currentYear--;
        }
        fillCalendar(currentMonth + 1, currentYear);
    });
    $('.calendarCell.next').click(function(){
        currentMonth = (currentMonth + 1) % 12;
        if(currentMonth == 0){
            currentYear++;
        }
        fillCalendar(currentMonth + 1, currentYear);
        // haal zooi op uit de google calendar
        // ....
        /*$.ajax({
            url: '/4happyfeet/http-docs/agenda/GetEvents',
            dataType: 'json',
            data: {},
            success: function(data){
                for(var i = 0; i<data.length; i++){
                    addEvent(data[i].date, data[i].start, data[i].end, data[i].title, data[i].description, data[i].location);
                }
                fillCalendar(currentMonth + 1, currentYear);
            }
        });*/
    });
    $('.event').live('mouseover', function(){
        var day = $(this).html();
        for(var i=0; i<data.length; i++){
            if(data[i].value == day && data[i].active){
                var e = data[i].e;
                // pas de waarden van de tooltip aan..
                $('#tt_title').html(e.caption);
                $('#tt_time').html(e.start + ' - ' + e.end);
                $('.tooltipLocation').hide();
                if(e.location != ''){
                    $('.tooltipLocation').show();
                    $('#tt_location').html(e.location);
                }
                $('#tt_description').html(e.description);
                // laat de tooltip verschijnen...
                var tt = $('#tooltip');
                var os = $(this).offset();
                var left = os.left - tt.width() - 24 /* padding */;
                var top = Math.round(os.top + ($(this).height() / 2) - ((tt.height()+24) / 2));
                var leftCorrection = (window.innerWidth - 850) / 2;
                var topCorrection = 20;
                left -= leftCorrection;
                top -= topCorrection;
                tt.stop(true, true);
                tt.css('left', left + 'px');
                tt.css('top', top + 'px');
                tt.animate({
                    opacity: 1,
                    left: '-=30'
                },{
                    duration: 'fast',
                    easing: 'easeOutQuad'
                });
            }
        }
    });
    $('.event').live('mouseout', function(){
        var tt = $('#tooltip');
        tt.stop(true, false);
        tt.animate({
            opacity: 0,
            left: '-=30'
        },{
            duration: 'fast',
            easing: 'easeInQuad',
            complete: function(){
                tt.css('top', '-1000px');
                tt.css('left', '-1000px');
            }
        });
    });
});

function DateCell(value, active, e, date, isToday){
    this.value = value;
    this.active = active;
    this.e = e;
    this.date = date;
    this.isToday = isToday;
}

function dateToString(d, m, y){
    // klopt niet helemaal: controleer of waarden negatief zijn..
    return d + '-' + m + '-' + y;
}

var data = new Array();

function fillCalendar(month, year){
    var now = new Date();
    var today = -1;
    if(month-1 == now.getMonth() && year == now.getFullYear()){
        today = now.getDate();
    }
    var daysInMonth = new Date(year, month, 0).getDate();
    var daysInPrevMonth = new Date(year, (month + 11) % 12, 0).getDate();
    var firstWeekDay = new Date(year, month-1, 1).getDay();

    // week begint op maandag, niet op zondag...
    firstWeekDay = (firstWeekDay + 6) % 7;
    data = new Array();
    var totalCount = 0;
    for(var i=daysInPrevMonth-firstWeekDay; i<daysInPrevMonth; i++){
        totalCount++;
        data.push(new DateCell(i+1, false, null, dateToString(i+1, month-1, year), false));
    }
    for(var d=1; d<=daysInMonth; d++){
        totalCount++;
        var e = findEvent(d, month, year);
        var isToday = (d == today);
        data.push(new DateCell(d, true, e, dateToString(d, month, year), isToday));
    }
    for(var r=totalCount; r < 42; r++){
        data.push(new DateCell(r-totalCount+1, false, null, dateToString(r-totalCount+1, month+1, year), false));
    }
    // zet de data nu in de calender
    var counter = 0;
    $('.calendarCell.entry').each(function(){
        $(this).html(data[counter].value);
        $(this).removeClass('inactive');
        $(this).removeClass('event');
        $(this).removeClass('today');
        $(this).e = null;
        if(!data[counter].active){
            $(this).addClass('inactive');
        }
        if(data[counter].e != null){
            $(this).addClass('event');
        }
        if(data[counter].isToday){
            $(this).addClass('today');
        }
        counter++;
    });
    $('.calendarCell.month').html(maanden[month-1] + ' ' + year);
}

var events = new Array();

function Event(date, start, end, caption, description, location, vrijdansen){
    this.date = date;
    this.start = start;
    this.end = end;
    this.caption = caption;
    this.description = description;
    this.location = location;
    this.vrijdansen = vrijdansen;

    // constructor..
    var chunks = date.split('-');
    this.d = chunks[0];
    this.m = chunks[1];
    this.y = chunks[2];

    this.isGreaterThan = function(d, m, y){
        return (y > this.y) || (m > this.m && y == this.y) || (d > this.d && m == this.m && y == this.y);
    }

    this.isEqualToOrGreaterThan = function(d, m, y){
        return this.isGreaterThan(d, m, y) || this.isEqualTo(d, m, y);
    }

    this.isLessThan = function(d, m, y){
        return (y < this.y) || (m < this.m && y == this.y) || (d < this.d && m == this.m && y == this.y);
    }

    this.isEqualToOrLessThan = function(d, m, y){
        return this.isLessThan(d, m, y) || this.isEqualTo(d, m, y);
    }

    this.isEqualTo = function(d, m, y){
        return d == this.d && m == this.m && y == this.y;
    }
}

function addEvent(date, start, end, caption, description, location, vrijdansen){
    events.push(new Event(date, start, end, caption, description, location, vrijdansen));
}

function findEvent(d, m, y){
    for(var i=0; i<events.length; i++){
        if(events[i].isEqualTo(d, m, y))
            return events[i];
    }
    return null;
}

//addEvent("19-3-2011", "20:00", "21:00", "Test1", "Dit is de eerste test, zonder locatie", "");
addEvent("24-3-2011", "20:00", "21:00", "Test2", "Dit is de tweede test, met locatie", "Verweggistan");